package Page.PersonalInformationPage;

import Controller.LoginAndExitException.NoUserLoginException;
import Controller.MainController;
import Controller.User.StudentUser;
import Dao.StudentDao;
import DataClass.Student;
import Navigation.NavigationController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import Page.MainPage.MainPageViewModel;

import java.net.URL;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ResourceBundle;

public class StudentPersonalInformationController implements Initializable {
    @FXML
    public TextField SNoField;
    @FXML
    public TextField SCnameField;
    @FXML
    public TextField STypeField;
    @FXML
    public TextField SEnterField;
    @FXML
    public TextField STNoField;
    @FXML
    public TextField SATNoField;
    @FXML
    public TextField SRemarkField;
    @FXML
    public TextField SInField;
    @FXML
    public TextField SOutField;
    @FXML
    public TextField SPasswordField;
    @FXML
    public Button EditButton;
    @FXML
    public Button LogoutButton;
    @FXML
    public Button SaveButton;
    @FXML
    private Button BackButton;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Student user;

        // 从登录系统获取学生信息
        try {
            user = StudentDao.getStudent(((StudentUser)MainController.getCurrentUser()).getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        SNoField.setText(user.getSNo());
        SNoField.setEditable(false);

        SCnameField.setText(user.getSCName());
        SCnameField.setEditable(false);

        STypeField.setText(user.getSType());
        STypeField.setEditable(false);

        SEnterField.setText(String.valueOf(user.getSEnter()));
        SEnterField.setEditable(false);

        STNoField.setText(user.getSTNo());
        STNoField.setEditable(false);

        SATNoField.setText(user.getSATNo());
        SATNoField.setEditable(false);

        SRemarkField.setText(user.getSRemark());
        SRemarkField.setEditable(false);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp timestamp1 = user.getSIn();
        if (timestamp1 == null)
            SInField.setText(null);
        else{
            String formattedDate1 = dateFormat.format(timestamp1);
            SInField.setText(formattedDate1);
        }
        SInField.setEditable(false);

        Timestamp timestamp2 = user.getSOut();
        if (timestamp2 == null)
            SInField.setText(null);
        else{
            String formattedDate2 = dateFormat.format(timestamp2);
            SOutField.setText(formattedDate2);
        }
        SOutField.setEditable(false);

        SPasswordField.setText(user.getSPassword());
        SPasswordField.setEditable(false);
    }

    public void backButtonClicked(ActionEvent actionEvent) {
        NavigationController.back();
    }

    public void logoutButtonClicked() {
        MainController.logout();
        MainPageViewModel.updateHelloText();
        NavigationController.back();
    }

    public void editButtonClicked () {
        // 在编辑按钮点击时启用文本框
        SPasswordField.setEditable(true);
    }
    public void saveButtonClicked () {
        //完成数据库更新
        String SNo = SNoField.getText();
        String SPassword = SPasswordField.getText();

        try {
            StudentDao.updatePassword(SNo,SPassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // 在保存按钮点击后禁用文本框
        SPasswordField.setEditable(false);
    }
}
